package com.drivers.homepage.controller;

import cn.hutool.core.util.ObjUtil;
import com.drivers.historyorder.controller.HistoryOrderController;
import com.drivers.historyorder.service.HistoryOrderService;
import com.drivers.homepage.domain.DriverOnceOrder;
import com.drivers.homepage.domain.OrderCost;
import com.drivers.homepage.domain.OrderInformation;
import com.drivers.homepage.mapper.DriverOnceOrderMapper;
import com.drivers.homepage.mapper.OrderCostMapper;
import com.drivers.homepage.mapper.OrderInformationMapper;
import com.drivers.homepage.service.OrderInformationService;
import com.drivers.model.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.annotation.RabbitListeners;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/HomePageOrder")
public class GetOrderController {

	private static final Logger log = LoggerFactory.getLogger(GetOrderController.class);
	@Autowired
	private OrderInformationService orderInformationService;
	@Autowired
	private RabbitTemplate rabbitTemplate;
	@Autowired
	private OrderCostMapper orderCostMapper;
	@Autowired
	RedisTemplate redisTemplate;
	@Autowired
	DriverOnceOrderMapper driverOnceOrderMapper;


	@Autowired
	private OrderInformationMapper orderInformationMapper;

//	听取订单
	@RequestMapping("/ListeningOrder")
	public R getOrder() {
//		模拟数据监听一条订单信息进行司机首页接单展示
		Object o = redisTemplate.opsForValue().get("orderProcessing");
		boolean aNull = ObjUtil.isNull(o);
		if(!aNull){
			String id = o.toString();
			OrderInformation orderList = orderInformationService.getOrder(Integer.parseInt(id));
			return R.ok("order", orderList);
		}else {
			OrderInformation orderInformation = new OrderInformation();
			return R.error().put("order",orderInformation);
		}






	}

	@RequestMapping("getOrderById")
	public R getOrderById(String id) {
		OrderInformation orderInformation = orderInformationService.getOrder(Integer.parseInt(id));
		return R.ok("order", orderInformation);
	}

//	点击接单后通过mq发送消息给用户告诉用户我已经接单了
	@GetMapping("upToUser")
	public R upToUser(String id) {
		// 发送消息到消息队列
		rabbitTemplate.convertAndSend("DriverTakesOrders", 1);
		log.info("发送消息成功",id);
		return R.ok("upToUser", "upToUser");
	}

	//点击接到乘客后修改当前司机状态
	@GetMapping("updateOrderStatus")
	public R updateOrderStatus(String id) {
//		修改司机的当前状态
		orderInformationService.updateOrderStatus(id);
//		去数据库查找当前订单的随机码
//		废除这个再次查询的操作,因为在页面初始化的时候已经查询过了
		return R.ok("upToUser", "upToUser");
	}


//	获取账单并加入账单内容
	@PostMapping("billsOfCharge")
	public R getBill(@RequestBody OrderCost orderCost) {

		if (null==orderCost.getHighwayFee()) {
			orderCost.setHighwayFee(new BigDecimal(0));
		}
		if (null==orderCost.getParkingFee()) {
			orderCost.setParkingFee(new BigDecimal(0));
		}
		if (null==orderCost.getAdditionalFee()) {
			orderCost.setAdditionalFee(new BigDecimal(0));
		}
		if (null==orderCost.getBridgeFee()) {
			orderCost.setBridgeFee(new BigDecimal(0));
		}
		int i = orderCostMapper.updateByPrimaryKeySelective(orderCost);
		OrderCost orderCost1 = orderCostMapper.selectByOrderId(orderCost.getOrderId());
		BigDecimal bigDecimal = new BigDecimal(0);
		bigDecimal=bigDecimal.add(orderCost.getHighwayFee());
		bigDecimal=bigDecimal.add(orderCost.getParkingFee());
		bigDecimal=bigDecimal.add(orderCost.getAdditionalFee());
		bigDecimal=bigDecimal.add(orderCost.getBridgeFee());
		bigDecimal=bigDecimal.add(orderCost1.getOrderMoney());

		orderCost1.setOrderMoney(bigDecimal);
		orderCostMapper.updateMoney(orderCost1);


		if(i>0){
			return R.ok("orderCost", orderCost);
		}else {
			return R.error("未知异常");
		}

	}
//通过id去找订单的账单信息
	@GetMapping("getOrderCost")
	@Transactional
	public R getOrderCost(String orderId) {
		BigDecimal getmoney = orderInformationService.getmoney(orderId);
//		根据公里数计算价格

		orderCostMapper.updatePassengerTotal(getmoney,orderId);

//		order_money
		OrderCost orderCost = orderCostMapper.selectMoney(orderId);




//		修改订单状态为已完成未支付
		orderInformationMapper.updateOrderInformation(orderId);
//		到达目的地时间
		orderCostMapper.updateDestinationTime(orderId,LocalDateTime.now());
		return R.ok("orderCost", orderCost);
	}




	//通过id去找订单的账单信息
	@GetMapping("getOrderCosts")
	public R getOrderCosts(String orderId) {
//		BigDecimal getmoney = orderInformationService.getmoney(orderId);
//		passenger_total
		OrderCost orderCost = orderCostMapper.selectMoney(orderId);

		//乘客支付金额
		orderCost.setPassengerTotal(orderCost.getOrderMoney());
		//司机收益
		BigDecimal getdrivermoney = orderInformationService.getdrivermoney(orderCost.getPassengerTotal());
		orderCost.setDriverEarning(getdrivermoney);
		//平台收益
		orderCost.setPlatformRevenue(orderCost.getPassengerTotal().subtract(getdrivermoney));

		orderCostMapper.update(orderCost);
		return R.ok("orderCost", orderCost);
	}


//	通过司机id去查询单次接单的所有数据:按照项目设计的第一种思路实现
	@GetMapping("getOrderOneTimeItem")
	public R getOrderInformation(String driverId) {
		DriverOnceOrder driverOnceOrder = orderInformationService.getOrderOneTimeItem(driverId);
//		DriverOnceOrder driverOnceOrder1=driverOnceOrderMapper.selectByDriverId(driverOnceOrder.getDriverId());
//		driverOnceOrder.setOnecePrice(driverOnceOrder1.getOnecePrice());
		return R.ok("orderInformation", driverOnceOrder);
	}

//	通过司机id去创建当日订单数据
	@GetMapping("createTodayOrderItem")
	public R createTodayOrderItem(String driverId) {
		Integer todayOrderItem = orderInformationService.createTodayOrderItem(driverId);
		return R.ok("todayId",todayOrderItem);
	}


//	点击休息修改订单状态
	@GetMapping("updateTodayOrderStatus")
	public R updateTodayOrderStatus(String id) {
//修改订单状态为1被接单
		Integer i = orderInformationService.updateTodayOrderStatus(id);
		Integer i1 = driverOnceOrderMapper.updateMoney(id);
		if(i>1){
			return R.ok("ok");
		}else {
			return R.error("更新状态失败!");
		}

	}
	//	接单后+1接单数
	@GetMapping("updateOrderNum")
	public R updateOrderNum(String id,String driverId) {
		Integer i = orderInformationService.updateNum(id);
		////		修改订单的司机id为接单的司机id
		orderInformationMapper.updateDriverId(id,Integer.parseInt(driverId));

//		发送消息给用户告诉她我接单了
		rabbitTemplate.convertAndSend("DriverTakesOrders", 1);
		if(i>1){
			return R.ok("ok");
		}else {
			return R.error("更新状态失败!");
		}

	}

	/**
	 *
	 * 接到单修改状态和接单时间
	 * @param orderId
	 * @return
	 */
	@RequestMapping("updateorderTime")
	@Transactional
	public R updateorderTime(String orderId) {
		//		修改订单状态为已完成未支付

//		接到订单的时间
		orderInformationMapper.updateOrderStatus(orderId,1);
		orderCostMapper.updateorderTime(orderId,LocalDateTime.now());
		return R.ok("ok");
	}

	/**
	 * 更新到达时间
	 * @param orderId
	 * @return
	 */
	@RequestMapping("updateArriveTime")
	public R updateArriveTime(String orderId) {
		//		修改订单状态为已完成未支付

//		接到订单的时间
		orderCostMapper.updateArriveTime(orderId,LocalDateTime.now());
		return R.ok("ok");
	}


	/**UpdateReceiveTime
	 * 接到乘客时间
	 */
	@RequestMapping("UpdateReceiveTime")
	public R UpdateReceiveTime(String orderId) {
		//		修改订单状态为已完成未支付

//		接到订单的时间
		orderCostMapper.UpdateReceiveTime(orderId,LocalDateTime.now());
		return R.ok("ok");
	}


	//t司机单击接单
	@RequestMapping("theDriverClicksTakeOrder")
	//参数订单编号 司机id
	public R updateOrders(String orderId,Integer driverId) {
		orderInformationService.updateOrders(orderId,driverId);
		return R.ok();
	};
	//司机到达起点
	@RequestMapping("theDriverArrivesAtTheStartingPoint")
	//参数订单编号
	public R updateGet(String orderId) {
		orderInformationService.updateGet(orderId);
		rabbitTemplate.convertAndSend("ReachTheDestination",1);
		return R.ok();
	};

	//司机接到乘客
	@RequestMapping("theDriverPicksUpThePassengers")
	//参数订单编号
	public R updateReceive(String orderId) {
		orderInformationService.updateReceive(orderId);
		return R.ok();
	};

	@RabbitListener(queues = "orderProcessing")
	public void receiveMessage(String message) {
//		存储redis中这个消息的信息,这个消息就是乘客发起订单后司机接收的订单信息
		redisTemplate.opsForValue().set("orderProcessing",message,5, TimeUnit.MINUTES);
		log.info("接收到消息:{}",message);
	}


}
